package com.naiterui.ehp.bs.pharmacist.service.impl;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.pharmacist.entity.Pharmacist;
import com.naiterui.ehp.bs.pharmacist.exception.ExceptionCodes;
import com.naiterui.ehp.bs.pharmacist.repository.mysql.PharmacistRepository;
import com.naiterui.ehp.bs.pharmacist.service.ITencentDetectService;
import com.naiterui.ehp.bs.pharmacist.utils.Constants;
import com.naiterui.ehp.bs.pharmacist.utils.PhTencentCloudSdkUtil;
import com.naiterui.ehp.bs.pharmacist.utils.PhTencentCloudSdkUtil.FaceSyncVO;
import com.naiterui.ehp.bs.pharmacist.vo.user.TencentFaceIdVO;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author guoyongxiang
 * @date 2019/10/19 10:31
 * @since 1.0.0
 */
@Service
public class PharmacistTencentDetectServiceImpl implements ITencentDetectService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PharmacistTencentDetectServiceImpl.class);
    /**
     * 腾讯云人脸识别orderNo
     */
    private static final String RDS_TENCENT_ORDER_NO = Constants.RDS_DETECT + "orderNo_";

    private static final String USER_ID_PRE = "PH";
    /**
     * 腾讯云人脸识别缓存有效期(秒)
     */
    private static final Integer RDS_TENCENT_CACHE_EXPIRE_SECOND = 60 * 5;

    @Autowired
    private PharmacistRepository pharmacistRepository;

    @Override
    public TencentFaceIdVO getFaceId(Long userId) throws BusinessException {
        Pharmacist pharmacist = pharmacistRepository.get(userId);
        return this.getFaceId(pharmacist.getId(), pharmacist.getName(), pharmacist.getCardNo());
    }

    @Override
    public TencentFaceIdVO getFaceId(String phone, String cardNo) throws BusinessException {
        Pharmacist pharmacist = pharmacistRepository.findFirstByPhoneAndCardNo(phone, cardNo);
        if (pharmacist == null) {
            LOGGER.warn("当前用户实名身份信息未录入 phone:{}, cardNo:{}", phone, cardNo);
            throw new BusinessException(ExceptionCodes.INVALID_REQUEST, "当前用户实名身份信息未录入");
        }
        return this.getFaceId(pharmacist.getId(), pharmacist.getName(), pharmacist.getCardNo());
    }

    private TencentFaceIdVO getFaceId(Long userId, String name, String cardNo) throws BusinessException {
        if (StringUtils.isBlank(cardNo) || StringUtils.isBlank(name)) {
            LOGGER.warn("当前用户实名信息未填写完整 userId:{}", userId);
            throw new BusinessException(ExceptionCodes.INVALID_REQUEST, "当前用户实名信息未填写完整");
        }

        String orderNo = UUID.randomUUID().toString().replaceAll("-", "");
        RedisUtil.valueOps().set(RDS_TENCENT_ORDER_NO + userId, orderNo, RDS_TENCENT_CACHE_EXPIRE_SECOND);
        return PhTencentCloudSdkUtil.getFaceid(orderNo, name, cardNo, USER_ID_PRE + userId);
    }

    @Override
    public FaceSyncVO getFaceSync(Long userId) throws BusinessException {
        String orderNo = RedisUtil.valueOps().getString(RDS_TENCENT_ORDER_NO + userId);
        RedisUtil.keyOps().delete(RDS_TENCENT_ORDER_NO + userId);
        if (StringUtils.isBlank(orderNo)) {
            return PhTencentCloudSdkUtil.FaceSyncVO.builder().result(false).build();
        }

        PhTencentCloudSdkUtil.FaceSyncVO faceSyncVO = PhTencentCloudSdkUtil.getFaceidSync(orderNo, USER_ID_PRE + userId);
        if (faceSyncVO.getResult()) {
            LOGGER.info("腾讯云人脸实名认证成功,设置进度状态 userId:{}", userId);
        }
        return faceSyncVO;
    }
}
